package com.springmvc.stduy.controller;

import com.springmvc.stduy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 若若叶
 * @date: 2021/9/23 15:10 FileName: TestController Description:
 */
@Controller
public class TestController {

  @RequestMapping("/login")
  public String toLoginPage() {
    return "login";
  }

  @RequestMapping("test1")
  public void test1(String name, Integer age) {
    System.out.println(name);
    System.out.println(age);
  }

  @RequestMapping("test2")
  public void test2(HttpServletRequest request) {
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    System.out.println(name);
    System.out.println(password);
  }

  @RequestMapping("test3")
  public void test3(User user) {

    System.out.println(user);
  }

  @RequestMapping("test4")
  public ModelAndView test4(ModelAndView modelAndView) {
    modelAndView.setViewName("test4");
    modelAndView.addObject("name", "lisi");
    modelAndView.addObject("password", "123456");
    return modelAndView;
  }

  @RequestMapping("test5")
  public String test5(Model model) {
    model.addAttribute("name", "admin");
    model.addAttribute("password", "admin");
    return "test4";
  }

  @RequestMapping("/test6")
  public void t6(HttpServletResponse response) {
    PrintWriter writer = null;
    try {
      writer = response.getWriter();
    } catch (IOException e) {
      e.printStackTrace();
    }
    writer.println("<script>alert('请先登录');<script>");
    writer.close();
  }

  @RequestMapping("/test7")
  public String test7(HttpServletRequest request) {
    User user = new User("zhangsan", "123546");
    request.setAttribute("user", user);
    return "test4";
  }

  @RequestMapping("/test8")
  public String test8(HttpSession session) {
    User user = new User("lisi", "123546");
    session.setAttribute("uuser", user);
    session.setAttribute("uid", 1);
    return "test4";
  }

  @RequestMapping("/test9")
  public String test9(HttpServletRequest request) {
    ServletContext servletContext = request.getServletContext();
    User user1 = new User("zhangsan", "123546");
    User user2 = new User("lisi", "123546");
    List<User> list = new ArrayList<>();
    list.add(user1);
    list.add(user2);
    servletContext.setAttribute("users", list);
    return "test4";
  }

  @RequestMapping("/test10")
  public void test10(HttpServletResponse response) {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter writer = null;
    try {
      writer = response.getWriter();
    } catch (IOException e) {
      e.printStackTrace();
    }
    writer.write("<script> alert('登录成功');location.href='http://www.baidu.com'; </script>");
  }

  @RequestMapping("/checkTel")
  public Map<String, Object> checkTel(String phone) {
    Map<String, Object> map = new HashMap<>();
    if ("12312341234".equals(phone)) {
      map.put("state", 201);
      map.put("msg", "手机号已存在");
      return map;
    }
    map.put("state", 200);
    map.put("msg", "手机号可以");
    return map;
  }
}
